package org.keyyond.ms.business.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.keyyond.ms.business.model.MsBusinessDocument;
import org.keyyond.ms.business.model.MsBusinessMessageLog;
import org.keyyond.ms.business.service.MsBusinessDocumentService;
import org.keyyond.ms.business.util.Constant;
import org.keyyond.ms.business.util.CommonBeanutils;
import org.keyyond.ms.core.util.ConstantResult;
import org.keyyond.ms.core.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.util.StringUtil;

/**
 * @author gaoB
 * @belong to Shccs
 * @date:2018-1-19(V1.0)
 * @date:2018-1-19(V1.0)
 * @version : V1.0
 */
@RequestMapping("doc")
@RestController
public class MsBusinessDocumentController extends CommonBeanutils {
	
	@Value("${documentYear}")
	private String documentYear;
	@Value("${documentMonth}")
	private String documentMonth;
	
    @Autowired
    private MsBusinessDocumentService documentService;
    
    /**
   	 * 记录日志的方法
   	 * 
   	 * @author gaoB
   	 * @since 2018-5-3
   	 * @category </br>记录日志的方法</br>
   	 *           
   	 * @param jsonStr
   	 *            | String | 必填 jsonStr 日志对象json字符串
   	 * @return <b>{日志主键:成功},{fail:失败},{exception:异常}</br>
   	 * @exception 
   	 */
    @RequestMapping(value = "/addLog", method = RequestMethod.POST)
    public String smsAddMessageLog(String param) {
	return null;
    }

    /**
   	 * 获取设置的年天数和月天数
   	 * 
   	 * @author GaoB
   	 * @since 2018-5-3
   	 * @category </br>获取设置的年天数和月天数</br>
   	 *           
   	 * @param request
   	 *            | HttpServletRequest | 必填  
   	 * @param response
   	 *            | HttpServletResponse | 必填  
   	 * @return <b>String</br>
   	 * @exception 
   	 */
    @RequestMapping(value = "/getDocument", method = RequestMethod.POST)
    public String getDocument(HttpServletRequest request, HttpServletResponse response) {
	response.setHeader("Access-Control-Allow-Origin", "*");
	String result = new String();
	JSONObject resultData = new JSONObject();
	String traceId = request.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
	MsBusinessMessageLog messageLog = new MsBusinessMessageLog();
	messageLog.setTraceid(traceId);
	messageLog.setLogtype(Constant.CONST_MESSAGE_LOG_EXCEPTION);
	String param = request.getParameter(Constant.CONST_ZBGS_PARAM);
	List<MsBusinessDocument> docs = new ArrayList<MsBusinessDocument>();
	try {
	    Map<String, String> paramMap = JsonUtil.JsonToMap(JSONObject.fromObject(param).toString());
		MsBusinessDocument docYear = documentService.selectByDocCode(documentYear);
		MsBusinessDocument docMonth = documentService.selectByDocCode(documentMonth);
		docs.add(docYear);
		docs.add(docMonth);
		resultData.put("record", docs);
		result = returnResult(true, resultData);
		messageLog.setLogtype(Constant.CONST_MEESAGE_LOG_SERVICE);
	} catch (Exception e) {
	    e.printStackTrace();
	    resultData.put(ConstantResult.RESULT_ERRMSG, ConstantResult.RESULT_ERRMSG_EXCEPTION);
	    resultData.put(ConstantResult.RESULT_ERRCODE, ConstantResult.RESULT_ERRCODE_EXCEPTION);
	    result = returnResult(false, resultData);
	}
	messageLog.setReturnmessage(result);
	addMessageLog(messageLog);
	return result;
    }

    /**
   	 * 更改年天数和月天数
   	 * 
   	 * @author gaoB
   	 * @since 2018-5-3
   	 * @category </br>更改年天数和月天数</br>
   	 *           
   	 * @param request
   	 *            | HttpServletRequest | 必填  
   	 * @return <b>String</br>
   	 * @exception 
   	 */
    @RequestMapping(value = "/updateDocument", method = RequestMethod.POST)
    public String updateByPrimaryKeySelective(HttpServletRequest request) {
	String param = request.getParameter(Constant.CONST_ZBGS_PARAM);
	return documentService.updateByPrimaryKeySelective(param);

    }

}
